package com.qf.config;
import com.qf.filter.TokenFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;

/**
 * 作者：熊剑
 * 时间：2023/10/16
 * 描述：
 */

@Configuration
@EnableWebSecurity
//@EnableGlobalMethodSecurity(prePostEnabled = true)//开启方法授权
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    /*自定义usernamePasswordAuthenticationFilter，登录流程*/
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        super.configure(auth);
    }

    /*放行静态资源*/
    @Override
    public void configure(WebSecurity web) throws Exception {
        super.configure(web);
    }

    /*自定义FilterSecurityInterceptor行为
    * 配置黑白名单*/
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests()
                .antMatchers("/user/login","/user/verifyToken","/user/register","/device/list")//白名单
                .permitAll()
                .anyRequest()
                .authenticated();

        //禁用csrFilter
        http.csrf().disable();

        //允许跨域访问
        http.cors();

        //向过滤器链中添加过滤器
        http.addFilterBefore(new TokenFilter(authenticationManager()), FilterSecurityInterceptor.class);
    }
}
